package pi.client.board;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import pi.client.core.MyProgram;
import pi.client.units.UnitBase;
import pi.client.utils.ImageUtils;

public class Cell {
	static final int TileGrass = 1;
	static final int TileGround = 2;
	static final int TileMountain = 3;
	static final int TileRiver = 4;

	public int costToWalk = 0;

	// private MyProgram program;

	public int tileType;
	public UnitBase unit;

	public int x;
	public int y;
	public Rectangle bounds;

	public BufferedImage image;

	static private BufferedImage tileGrass = ImageUtils
			.LoadImage("res/images/game/board/tiles/tileGrass.png");
	static private BufferedImage tileGround = ImageUtils
			.LoadImage("res/images/game/board/tiles/tileGround.png");
	static private BufferedImage tileMountain = ImageUtils
			.LoadImage("res/images/game/board/tiles/tileMountain.png");
	static private BufferedImage tileRiver = ImageUtils
			.LoadImage("res/images/game/board/tiles/tileRiver.png");

	public Cell(MyProgram program, int tileType, UnitBase unit, int x, int y,
			int width, int height) {
		// this.program = program;
		this.x = x;
		this.y = y;
		this.tileType = tileType;
		this.unit = unit;

		this.bounds = new Rectangle(x * width, y * height, width, height);

		RefreshCellByType(tileType);
	}

	public void RefreshCellByType(int tileType) {
		this.tileType = tileType;

		switch (tileType) {
		case TileGrass:
			image = tileGrass;
			costToWalk = 1;
			break;

		case TileGround:
			image = tileGround;
			costToWalk = 1;
			break;

		case TileMountain:
			image = tileMountain;
			costToWalk = 2;
			break;

		case TileRiver:
			image = tileRiver;
			costToWalk = 3;
			break;
		}
	}

	public String GetStringTileType() {
		switch (tileType) {
		case TileGrass:
			return "Grass";

		case TileGround:
			return "Ground";

		case TileMountain:
			return "Moutain";

		case TileRiver:
			return "River";
		}

		return null;
	}

	public void Draw(Graphics2D g) {
		g.drawImage(image, bounds.x, bounds.y, bounds.width, bounds.height,
				null);

		if (unit != null) {
			unit.Draw(g);
		}
	}

	public void Update(long timer) {
		if (unit != null) {
			unit.Update(timer);
		}
	}
}